package view.panel;

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;

import manager.OrderManager;
import vo.Order;

public class OrderPanel extends JPanel {

	JTextField jfieldYear, jfieldMonth, jfieldDay, jfieldCustomId;
	JLabel jlabelYear, jlabelMonth, jlabelDay, jlabelCustomId, jlabelMenu;
	ArrayList<JCheckBox> menuList;
	JButton jbtnOrder, jbtnCancel;
	Date currentDate;
	
	public OrderPanel() {

		setLayout(new FlowLayout(FlowLayout.LEFT, 10, 30));
		setSize(300,200);
		setVisible(true);

		Calendar cal = Calendar.getInstance();
		
				
		jfieldYear = new JTextField(String.valueOf(cal.get(Calendar.YEAR)),4);
		jfieldMonth = new JTextField(String.valueOf(cal.get(Calendar.MONTH)),4);
		jfieldDay = new JTextField(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)),4);
		jfieldCustomId = new JTextField("고객번호",4);
		
		jlabelYear = new JLabel("년");
		jlabelMonth = new JLabel("월");
		jlabelDay = new JLabel("일");
		jlabelCustomId = new JLabel("고객번호");
		jlabelMenu = new JLabel("메뉴");

		jbtnOrder = new JButton("주문");
		jbtnCancel = new JButton("취소");

		menuList = new ArrayList<JCheckBox>();

		addMenu("김밥");
		addMenu("떡볶이");
		addMenu("순대");
		addMenu("오뎅");
		addMenu("튀김");

		add(jfieldYear);
		add(jlabelYear);
		add(jfieldMonth);
		add(jlabelMonth);
		add(jfieldDay);
		add(jlabelDay);
		add(jlabelCustomId);
		add(jfieldCustomId);
		add(jlabelMenu);

		registMenu();

		add(jbtnOrder);
		add(jbtnCancel);

		jbtnOrder.addMouseListener(new MouseInputAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				int customId = Integer.parseInt(jfieldCustomId.getText());
				String year = jfieldYear.getText();
				String month = jfieldMonth.getText();
				String day = jfieldDay.getText();

				Order order = new Order(customId, year, month, day, checkedMenu());

				OrderManager.getInstance().createOrder(customId, order);
	
			}
		});

		jbtnCancel.addMouseListener(new MouseInputAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String blank = "";
				// UI 초기화
				jfieldCustomId.setText(blank);
				jfieldDay.setText(blank);
				jfieldMonth.setText(blank);
				jfieldYear.setText(blank);
				uncheckAllMenu();
				
				validate();
			}
		});	
	}

	private void addMenu(String menuName) {

		JCheckBox checkMenu = new JCheckBox(menuName);
		// 메뉴 리스트 등록
		menuList.add(checkMenu);		
	}

	private void registMenu() {

		// 메뉴 UI 들을 등록
		for(int i = 0; i < menuList.size(); ++i) {
			this.add(menuList.get(i));			
		}

	}

	private ArrayList<String> checkedMenu() {

		ArrayList<String> selected = new ArrayList<String>();

		for(int i = 0; i < menuList.size(); ++i) {

			if(menuList.get(i).isSelected())
				selected.add(menuList.get(i).getText());
		}

		return selected;
	}
	
	private void uncheckAllMenu() {
		
		for(int i = 0; i < menuList.size(); ++i) {
			menuList.get(i).setSelected(false);
		}
	}

}
